﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BattleshipContract;
using System.Drawing;

namespace Client1
{
    [Serializable]
    public class GameMaster : MarshalByRefObject
    {
        private List<Ship> shipList = new List<Ship>();
        //private List<Shot> shotList = new List<Shot>();
        private int index = -1, oldX = 0, oldY = 0;
        private bool isTaken = false;

        public GameMaster()
        {
            shipList.Add(new Ship("BATTLESHIP", "VERTICAL", 1, 1));
            shipList.Add(new Ship("DESTROYER", "VERTICAL", 1, 29));
            shipList.Add(new Ship("SUBMARINE", "VERTICAL", 1, 57));
            shipList.Add(new Ship("CARRIER SHIP", "VERTICAL", 1, 85));
            shipList.Add(new Ship("PATROL BOAT", "VERTICAL", 1, 113));
        }

        public void draw(System.Windows.Forms.PaintEventArgs paint)
        {
            foreach (Ship s in shipList)
                s.draw(paint);
            //foreach (Shot d in shotList)
            //    d.draw(paint);
        }

        /// <summary>
        /// check if coordinate is free or not
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns>true if free</returns>
        public bool isOccupiedByShip(int x, int y)
        {
            bool value = true;
            foreach (Ship s in shipList)
                if (s.isOccupied(x, y) == true)
                    value = false;
            return value;
        }

        /// <summary>
        /// check if there is another ship on selected tile
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns>true if tile is not free</returns>
        public bool OccupiedByOthers(int x, int y)
        {
            int counter = 0;

            foreach (Ship s in shipList)
                if (s.isOccupied(x, y) == true)
                    counter++;
            if (counter > 1)
                return true;
            else
                return false;
        }

        public int findShip(int x, int y)
        {
            for (int d = 0; d < shipList.Count; d++)
            {
                if (isTaken == false)
                {
                    if (shipList[d].isOccupied(x, y) == true)
                    {
                        index = d;
                        oldY = shipList[d].XCoordinate;
                        oldX = shipList[d].YCoordinate;
                        isTaken = true;
                    }
                }
            }

            if (index > -1)
                Console.WriteLine(shipList[index].ShipType + " is selected");

            return 0;
        }

        public void moveShip(int newX, int newY)
        {
            if (index != -1)
                shipList[index].changeCoordinate(newY, newX);
        }

        public void releaseShip(int x, int y)
        {
            isTaken = false;
            if (OccupiedByOthers(x, y) == true)
            {
                shipList[index].changeCoordinate(oldX, oldY);
            }

            index = -1;
        }
    }
}
